package cn.creat.zhxy.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import sun.security.krb5.internal.crypto.crc32;

import cn.creat.zhxy.po.CurrentRoom;
import cn.creat.zhxy.service.CurrentRoomService;
import cn.creat.zhxy.utils.httputil.MyHttpClient;

public class CurrentRoomServiceImpl implements CurrentRoomService{
	
	private static MyHttpClient client = new MyHttpClient();
	
	@Override
	public CurrentRoom[] getCurrentRoom(String listUrl,String roomUrl, int id) throws ClientProtocolException, IOException {
		CurrentRoom[] currentRooms = new CurrentRoom[4];
		for(int i = 0; i < currentRooms.length; i++){
			int j = i*2+1;
			currentRooms[i] = new CurrentRoom();
			currentRooms[i].setEqno(""+j+"-"+(j+1));
		}
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("json", true);
		CloseableHttpResponse response = client.sendPost(listUrl, map, "utf-8", false);
		HttpEntity entity = response.getEntity();
		JSONObject obj = JSONObject.fromObject(EntityUtils.toString(entity, "utf-8"));
		Object roomsJson = obj.get("Obj");
		JSONArray roomsArray = JSONArray.fromObject(roomsJson);
		for(int i = 0; i < roomsArray.size(); i++){
			JSONObject room = JSONObject.fromObject(roomsArray.get(i));
			String roomNum = room.getString("ROOMNUM");
			for(CurrentRoom currentRoom : currentRooms){
				if(!currentRoom.getRooms().contains(roomNum)){
					currentRoom.getRooms().add(roomNum);
				}
			}
		}
		response.close();
		CloseableHttpResponse responseRooms = client.sendPost(roomUrl, map, "utf-8", false);
		obj = JSONObject.fromObject(EntityUtils.toString(responseRooms.getEntity(), "utf-8"));
		roomsJson = obj.get("Obj");
		roomsArray = JSONArray.fromObject(roomsJson);
		for(int i = 0; i < roomsArray.size(); i++){
			JSONObject room = JSONObject.fromObject(roomsArray.get(i));
			if(!room.isEmpty()){
				String roomNum = room.getString("ROOMNUM");
				String eqno = room.getString("EQNO");
				if(eqno.equalsIgnoreCase("1-2")){
					if(currentRooms[0].getRooms().contains(roomNum)){
						currentRooms[0].getRooms().remove(roomNum);
					}
				}else if(eqno.equalsIgnoreCase("3-4")){
					if(currentRooms[1].getRooms().contains(roomNum)){
						currentRooms[1].getRooms().remove(roomNum);
					}
				}else if(eqno.equalsIgnoreCase("5-6")){
					if(currentRooms[2].getRooms().contains(roomNum)){
						currentRooms[2].getRooms().remove(roomNum);
					}
				}else {
					if(currentRooms[3].getRooms().contains(roomNum)){
						currentRooms[3].getRooms().remove(roomNum);
					}
				}
			}
		}
		return currentRooms;
		
	}
	
	@Override
	public Map<Integer, CurrentRoom[]> getAllCurrentRoom(String listUrl,String roomUrl, int[] ids) throws Exception{
		Map<Integer, CurrentRoom[]> map = new HashMap<>();
		for(int id : ids){
			CurrentRoom[] currentRooms = getCurrentRoom(listUrl, roomUrl, id);
			map.put(id, currentRooms);
		}
		return map;
		
	}
	public static void main(String[] args) throws Exception {
		CurrentRoomService currentRoomService = new CurrentRoomServiceImpl();
		CurrentRoom[] result = currentRoomService.getCurrentRoom("http://jwkq.xupt.edu.cn:8080/Room/GetListByBuild","http://jwkq.xupt.edu.cn:8080/Room/GetCurrentRoom", 166);
		for(CurrentRoom room : result){
			System.out.print(room.getEqno()+" ");
			for(String s : room.getRooms()){
				System.out.print(s+" ");
			}
			System.out.println();
		}
	}
	

}
